package indi.jp.project.business.device.service.impl;

import indi.jp.project.business.device.dao.DeviceDAO;
import indi.jp.project.business.device.entity.dto.DeviceHistoryDTO;
import indi.jp.project.business.device.entity.dto.DeviceChangeDTO;
import indi.jp.project.business.device.entity.Device;
import indi.jp.project.business.device.entity.DeviceHistory;
import indi.jp.project.business.device.entity.DevicePurchase;
import indi.jp.project.business.device.service.DeviceService;
import indi.jp.project.core.entity.Datagrid;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.util.SerialUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Resource
    private DeviceDAO deviceDAO;

    @Resource
    private SerialUtil serialUtil;

    @Override
    public JsonResult queryGrid(Device entity) throws Exception {
        List<Device> list = deviceDAO.queryList(entity);
        int count = deviceDAO.queryCount(entity);
        Datagrid<Device> datagrid = new Datagrid<>(list, count);
        return JsonResult.success(datagrid);
    }

    @Override
    public JsonResult save(Device entity) throws Exception {
        if (StringUtils.isBlank(entity.getId())) {
            String id = String.valueOf(serialUtil.nextId());
            entity.setId(id);
            deviceDAO.insert(entity);
        } else {
            deviceDAO.update(entity);
        }
        return JsonResult.success();
    }

    @Override
    public JsonResult deleteById(String id) throws Exception {
        deviceDAO.deleteById(id);
        return JsonResult.success();
    }

    @Override
    public JsonResult findById(String id) throws Exception {
        Device device = deviceDAO.findById(id);
        return JsonResult.success(device);
    }

    @Override
    public void newPurchaseDevice(DevicePurchase devicePurchase) throws Exception {
        int num = devicePurchase.getApplyNum();
        List<Device> list = new ArrayList<>();
        List<DeviceHistoryDTO> historyDTOS = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            Device device = new Device();
            String id = String.valueOf(serialUtil.nextId());
            device.setId(id);
            device.setName(devicePurchase.getDeviceName());
            device.setModel(devicePurchase.getDeviceModel());
            device.setDepartmentId(devicePurchase.getDepartmentId());
            device.setDepartmentCode(devicePurchase.getDepartmentCode());
            device.setDepartmentName(devicePurchase.getDepartmentName());
            device.setProjectId(devicePurchase.getProjectId());
            device.setProjectCode(devicePurchase.getProjectCode());
            device.setProjectName(devicePurchase.getProjectName());
            device.setCreateBy(devicePurchase.getCreateBy());
            list.add(device);

            DeviceHistoryDTO deviceHistoryDTO = new DeviceHistoryDTO();
            deviceHistoryDTO.setId(String.valueOf(serialUtil.nextId()));
            deviceHistoryDTO.setDeviceId(id);
            deviceHistoryDTO.setBillId(devicePurchase.getId());
            deviceHistoryDTO.setEventType("purchase");
            deviceHistoryDTO.setEventDesc("采购设备");
            deviceHistoryDTO.setCreateBy(devicePurchase.getCreateBy());
            historyDTOS.add(deviceHistoryDTO);
        }
        // 保存设备采购历史
        saveDeviceHistory(historyDTOS);
        // 保存到设备库
        deviceDAO.batchInsertDevice(list);
    }

    @Override
    public void updateDevice(List<DeviceChangeDTO> list) throws Exception {
        deviceDAO.updateDevice(list);
    }

    @Override
    public void saveDeviceHistory(List<DeviceHistoryDTO> list) throws Exception {
        deviceDAO.saveDeviceHistory(list);
    }

    @Override
    public JsonResult queryHistoryGrid(String deviceId) throws Exception {
        List<DeviceHistory> list = deviceDAO.queryHistoryList(deviceId);
        int count = deviceDAO.queryHistoryCount(deviceId);
        Datagrid<DeviceHistory> datagrid = new Datagrid<>(list, count);
        return JsonResult.success(datagrid);
    }
}
